/**
 * @(#) CongMoneyMediator.as
 *
 * @date		2009. 03. 25.
 * @version		1.0
 * @author		baek, kwang hyoun
 * @description	건강보험증 신청 - Mediator
 * 				건강보험증을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.healthchkreq.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.healthchkreq.HealthchkreqFacade;
	import com.skt.common_hr.healthchkreq.events.gwarekeyEvent;
	import com.skt.common_hr.healthchkreq.view.components.HealthchkreqOverViewSac;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class healthchkreqMediator extends OverViewMediator
	{
		public static const NAME:String = "healthchkreqMediator";


		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function healthchkreqMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		* 최초 한번만 불림
		*/
 		override protected function initializeView():void
 		{
			super.initializeView();
 		}
		/***
		 * 활성화가 될때마다 불림.
		 */
		override protected function activatedView():void
		{
			super.activatedView();
 			loadList();
			setEventListener();
 			view.vsMain.selectedIndex = 0;
		}

		override protected function closedView():void
		{
			removeEventListener();
		}

 		private function setEventListener():void
 		{
 			view.addEventListener(HealthchkreqFacade.HealthchkreqWrite, onClickNew);
			view.addEventListener(gwarekeyEvent.SELECT_DATA, onClickReadOnly);
			view.addEventListener(gwarekeyEvent.UPDATE_DATA, onClickModify);
			view.addEventListener(gwarekeyEvent.DELETE_DATA, onClickDelete);
			view.addEventListener(gwarekeyEvent.PRINT_DATA, onStatementView);
 		}

 		private function removeEventListener():void
 		{
			view.removeEventListener(HealthchkreqFacade.HealthchkreqWrite, onClickNew); 			
			view.removeEventListener(gwarekeyEvent.SELECT_DATA, onClickReadOnly);
			view.removeEventListener(gwarekeyEvent.UPDATE_DATA, onClickModify);
			view.removeEventListener(gwarekeyEvent.DELETE_DATA, onClickDelete);
			view.removeEventListener(gwarekeyEvent.PRINT_DATA, onStatementView);
 		}


 		private function loadList():void
 		{
// 			var mProxy:MedicalMoneyProxy = facade.retrieveProxy(MedicalMoneyProxy.NAME) as MedicalMoneyProxy;
//			mProxy.loadList(Variables.myPernr);
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					// reload
//					ReqMedicalMoneyMediator.CLOSED_REQUEST_VIEW,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_COMPLETE,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_FAILED
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				// 의료비 리스트  조회 Proxy 호출결과 처리
//				case MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_COMPLETE :
//					setListView(MedicalMoneyProxy(note.getBody()).mainList);
//					break;
//				// 의료비 리스트 조회 실패시
//				case MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_FAILED :
//					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_LOAD_LIST", null, "ALERT_TITLE_SYSTEM_ERROR");
//					break;
//
//				// 재조회
//				case ReqMedicalMoneyMediator.CLOSED_REQUEST_VIEW:
//					view.vsCompensation.selectedIndex = 0;
//					loadList();
//					break;

				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


 		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function setListView(list:ArrayCollection):void
 		{
// 			view.medicalMoneyDataList = list;
// 			commonFacade.setStatusMessage("STATUS_LOADED_LIST", [list.length.toString()]);
// 			if(list.length == 0) {
// 				view.showInitialTip();
// 			}
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		/**
		 * 클릭시 단순 조회
		 * @param
		 * @return void
		 */
 		private function onClickReadOnly(event:gwarekeyEvent):void
 		{
			var param:Object = new Object();
 			param.gwarekey  = event.key;
 			param.pernr = event.cpernr;
 			param.name  = Variables.myName;
 			param.write  = false;

 			//executeReqView(param);
 		}

 		/**
		 * 신규 신청 버튼 처리
		 * @param
		 * @return void
		 */
 		private function onClickNew(event:Event):void
 		{
 			var param:Object = new Object();
 			param.pernr 	= Variables.myPernr;
 			param.name  	= Variables.myName;
 			param.gwarekey  = "";
			param.write  	= true;

 			executeReqView(param);
 		}

 		/**
		 * 의료비 수정 버튼 클릭시 실행
		 * @param
		 * @return void
		 */
 		private function onClickModify(event:gwarekeyEvent):void
 		{
			var param:Object = new Object();
 			param.gwarekey  = event.key;
 			param.pernr = event.cpernr;
 			param.name  = Variables.myName;
			param.write  	= true;

 			executeReqView(param);
 		}

 		/**
		 * 신규 신청 및 수정 화면 실행
		 * @param
		 * @return void
		 */
 		private function executeReqView(param:Object):void
 		{
 			commonFacade.setStatusMessage("");
			view.vsMain.selectedIndex = 1;
			
 			var writeMediator:ReqHealthchkreqMediator = facade.retrieveMediator(ReqHealthchkreqMediator.NAME) as ReqHealthchkreqMediator;
 			if(!writeMediator) {
				writeMediator = new ReqHealthchkreqMediator(view.healthchkreqWriteView);
	 			facade.registerMediator(writeMediator);
			}
 			if(writeMediator) facade.sendNotification(HealthchkreqFacade.LOAD_HEALTHCHKREQ_WRITE_INITIAL, param);
 		}

 		/** 
		 * 삭제 버튼 클릭시 실행
		 * @param
		 * @return void
		 */
 		private function onClickDelete(event:gwarekeyEvent):void
 		{
 			//var mmProxy:MedicalMoneyProxy = facade.retrieveProxy(MedicalMoneyProxy.NAME) as MedicalMoneyProxy;
			//mmProxy.deleteData(event.key, event.wstatus);
 		}

 		/**
		 * 전표조회
		 * @param
		 * @return void
		 */
 		private function onStatementView(event:gwarekeyEvent):void
 		{
 			//var statementProxy:StatementUrlProxy = facade.retrieveProxy(StatementUrlProxy.NAME) as StatementUrlProxy;
			//statementProxy.load(event.key.toString());
 		}
 		 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		private function findCodeIndex(key:String, srcArr:ArrayCollection):int
		{
			for(var i:int=0; i<srcArr.length; i++) {
				if(srcArr[i].data == key) {
					return i;
				}
			}
			return -1; 
		}

       private function get view():HealthchkreqOverViewSac
        {
            return viewComponent as HealthchkreqOverViewSac;
        }


	}
}